package cn.machen.www.fashion.dao;

import cn.itcast.jdbc.TxQueryRunner;
import cn.machen.www.fashion.pojo.Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.management.relation.RoleUnresolved;
import java.sql.SQLException;
import java.util.List;

public class ProductDao {
    private QueryRunner queryRunner = new TxQueryRunner();

    /*查询所有商品*/
    public List<Product> findAll()  {
        String sql = "select * from products";
        try {
            return queryRunner.query(sql,new BeanListHandler<Product>(Product.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /*根据cid查询商品*/
    public List<Product> findByCid(String cid) {
        String sql = "select * from products where cid =?";
        try {
            return queryRunner.query(sql,new BeanListHandler<Product>(Product.class),cid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //根据pid查询单个商品
    public Product findSingleProduct(String pid){
        String sql = "select * from products where pid =?";
        try {
           return queryRunner.query(sql,new BeanHandler<Product>(Product.class),pid);
        } catch (SQLException e) {
            e.getStackTrace();
            throw new RuntimeException(e);
        }
    }



}
